﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Concurrent;
using RuyeeModbus.Device;
namespace RuyeeModbus
{
    public class ModbusTcpMaster : IMaster
    {

       
        private ModbusTcpMaster()
        {

        }
        public ModbusTcpMaster(UInt16 StartAddressMester, UInt16 EndAddressMester, byte ServerID)
        {
            this.ServerID = ServerID;
            this.StartAddressMester = StartAddressMester;
            this.EndAddressMester = EndAddressMester;
        }
        UInt16 StartAddressMester;
        UInt16 EndAddressMester;
        SICOMSocket.SocketAsyncServer SAC = new SICOMSocket.SocketAsyncServer();
        byte ServerID;
        public void Start()
        {

            SAC.ClientShutDownEnvent += new SICOMSocket.SocketAsyncServer.ClientShutDownDelegate(SAC_ClientShutDownEnvent);
            SAC.NewConnectEnvent += new SICOMSocket.SocketAsyncServer.NewConnectDelegate(SAC_NewConnectEnvent);
            SAC.ProcessMessageeEnvent += new SICOMSocket.SocketAsyncServer.ProcessMessageeDelegate(SAC_ProcessMessageeEnvent);
            SAC.Start(502);
        }

        void SAC_ProcessMessageeEnvent(System.Net.Sockets.Socket clientSock, byte[] msgBuff, DateTime getDataTime)
        {

            if (!CheckFunctionCodes(msgBuff[7]))
            {
                return;
            }
            ModbusFunctionCodes check = (ModbusFunctionCodes)msgBuff[7];
            switch (check)
            {
                case ModbusFunctionCodes.ReadCoils:
                    {
                        ReadCoils(clientSock, msgBuff, getDataTime);
                        break;
                    }
                case ModbusFunctionCodes.ReadDiscretionsInput:
                    {
                        ReadDiscretionsInput(clientSock, msgBuff, getDataTime);
                        break;
                    }

            }



        }
        void ReadCoils(System.Net.Sockets.Socket clientSock, byte[] msgBuff, DateTime getDataTime)
        {

        }

        void ReadDiscretionsInput(System.Net.Sockets.Socket clientSock, byte[] msgBuff, DateTime getDataTime)
        {

        }
        void ReadHoldingRegisters(System.Net.Sockets.Socket clientSock, byte[] msgBuff, DateTime getDataTime)
        {

        }
        void ReadInputRegisters(System.Net.Sockets.Socket clientSock, byte[] msgBuff, DateTime getDataTime)
        {

        }
        void WriteSingleCoil(System.Net.Sockets.Socket clientSock, byte[] msgBuff, DateTime getDataTime)
        {

        }
        void WriteSingleRegister(System.Net.Sockets.Socket clientSock, byte[] msgBuff, DateTime getDataTime)
        {

        }
        void WriteMultipleCoils(System.Net.Sockets.Socket clientSock, byte[] msgBuff, DateTime getDataTime)
        {

        }
        void WriteMultipleRegisters(System.Net.Sockets.Socket clientSock, byte[] msgBuff, DateTime getDataTime)
        {

        }
        void ReadFileRecord(System.Net.Sockets.Socket clientSock, byte[] msgBuff, DateTime getDataTime)
        {

        }
        void WriteFileRecord(System.Net.Sockets.Socket clientSock, byte[] msgBuff, DateTime getDataTime)
        {

        }
        void MaskWriteRegister(System.Net.Sockets.Socket clientSock, byte[] msgBuff, DateTime getDataTime)
        {

        }
        void ReadWriteMultipleRegisters(System.Net.Sockets.Socket clientSock, byte[] msgBuff, DateTime getDataTime)
        {

        }
        void ReadFIFOQueue(System.Net.Sockets.Socket clientSock, byte[] msgBuff, DateTime getDataTime)
        {

        }
        void EncapsulatedInterfaceTransport(System.Net.Sockets.Socket clientSock, byte[] msgBuff, DateTime getDataTime)
        {

        }
        bool CheckFunctionCodes(byte FunctionCode)
        {
            if (!Enum.IsDefined(typeof(ModbusFunctionCodes), FunctionCode))
            {
                throw new Exception<ModbusException>(new ModbusException(ModbusExceptionCode.IllegalFunction));
            }
            return true;
        }
        bool CheckAddress(byte StartAddressHigh, byte StartAddressLow, byte CountHigh, byte CountLow)
        {
            UInt16 StartAddress = Convert.ToUInt16((StartAddressHigh << 8) + StartAddressLow);
            UInt16 Count = Convert.ToUInt16((CountHigh << 8) + CountLow);
            if (0 < Count && Count < 0x7d1)
            {
                throw new Exception<ModbusException>(new ModbusException(ModbusExceptionCode.IllegalDataValue));
            }
            if (StartAddress < StartAddressMester || (EndAddressMester - Count) < StartAddressMester)
            {
                throw new Exception<ModbusException>(new ModbusException(ModbusExceptionCode.IllegalDataAddress));
            }
            return true;
        }
        void SAC_NewConnectEnvent(string IP, string port, System.Net.Sockets.Socket clinetSocket)
        {

        }

        void SAC_ClientShutDownEnvent(string IP, string port)
        {

        }



        public override bool AddDevice(IDevice Device)
        {
            throw new NotImplementedException();
        }

        public override ConcurrentQueue<DiscretionsInput> DiscretionsInput
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override ConcurrentQueue<Coils> CoilsList
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override ConcurrentQueue<InputRegisters> InputRegistersList
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override ConcurrentQueue<InputRegisters> HoldingRegisters
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
}
